#!/bin/bash
nginx_home=/opt/module/nginx
log_home=/home/atguigu/realtime
java_home=/opt/module/jdk1.8.0_212

case $1 in
"start")
  # 先开启nginx
  nginx=$(ps -ef | awk '/nginx/ && !/awk/ {print $n}')
  if [[ -z "$nginx" ]]; then
    ${nginx_home}/sbin/nginx
    echo "===== start nginx server: hadoop102 ====="
  else
    echo "nginx已经启动,请勿重复启动...."
  fi

  # 启动3个日志服务器
  for host in hadoop102 hadoop103 hadoop104; do
    startlog=$(ssh ${host} "ps -ef | grep gmall_logger-0.0.1-SNAPSHOT.jar | grep -v grep")
    if [[ -n "$startlog" ]]; then
      echo " $host 日志服务已经启动，请勿重复启动...."
    else
      echo "===== start logger server: ${host} ====="
      ssh $host "nohup ${java_home}/bin/java -jar ${log_home}/gmall_logger-0.0.1-SNAPSHOT.jar 1>${log_home}/error.log 2>&1 &"
    fi
  done
  ;;
"stop")
  # 关闭3个日志服务器
  for host in hadoop102 hadoop103 hadoop104; do
    stoplog=$(ssh ${host} "ps -ef | grep gmall_logger-0.0.1-SNAPSHOT.jar | grep -v grep")
    if [[ -n "$stoplog" ]]; then
      echo "===== stop logger server: ${host} ====="
      ssh $host "ps -ef | grep gmall_logger-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print \$2}'| xargs kill -9 >/dev/null 2>&1"
    else
      echo " $host 日志服务已经关闭，请勿重复关闭...."
    fi
  done
  # 关闭 nginx
  nginx=$(ps -ef | awk '/nginx/ && !/awk/ {print $n}')
  if [[ -n "$nginx" ]]; then
    echo "===== stop nginx server: hadoop102 ====="
    $nginx_home/sbin/nginx -s stop
  else
    echo " nginx已经关闭,请勿重复关闭...."
  fi
  ;;
*)
  echo "启动姿势不对"
  echo "log.sh start 启动日志采集工程"
  echo "log.sh stop  停止日志采集工程"
  ;;
esac
